package com.mhtvs;

import java.util.HashMap;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import com.mhtvs.TunnelInfo;

//---------------------------------------------------------------
//Development source for MHTVS CLIENT
//"Android client for use with MHTVS"
//Copyright (c) 2011, Doug Thompson, perladdict@gmail.com
//All rights reserved.
//
//http://code.google.com/p/mhtvs/
//
//View Demo here:
//http://www.youtube.com/watch?v=v4M2uVrMrGk
//
//Redistribution and use in source and binary forms, with or
//without modification, are permitted provided that the following
//conditions are met:
//
//* Redistributions of source code must retain the above
//  copyright notice, this list of conditions and the
//  following disclaimer.
//* Redistributions in binary form must reproduce the
//  above copyright notice, this list of conditions and
//  the following disclaimer in the documentation and/or
//  other materials provided with the distribution.
//* Neither the name of the developer nor the names
//  of its contributors may be used to endorse or promote
//  products derived from this software without specific
//  prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
//CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
//SUCH DAMAGE.
//---------------------------------------------------------------


	public class Tunnel {		
		public void go() throws Exception{		
						HashMap<String,String> sqliteConfsHash = TunnelInfo.getAllDetailsInHASH();
						String serverUserName = String.valueOf(sqliteConfsHash.get("serverUserName0")); 
						String serverAddress = String.valueOf(sqliteConfsHash.get("serverAddress0")); 
						int serverPort = Integer.parseInt(sqliteConfsHash.get("serverPort0")); 
						String masterPassword = String.valueOf(sqliteConfsHash.get("masterPassword0")); 
						String serverInternalAddress = String.valueOf(sqliteConfsHash.get("serverInternalAddress0")); 
						int portforwardsLOCAL1 = Integer.parseInt(sqliteConfsHash.get("portforwardsLOCAL0"));
						int portforwardsREMOTE1 = Integer.parseInt(sqliteConfsHash.get("portforwardsREMOTE0"));
						int portforwardsLOCAL2 = Integer.parseInt(sqliteConfsHash.get("portforwardsLOCAL1")); 
						int portforwardsREMOTE2 = Integer.parseInt(sqliteConfsHash.get("portforwardsREMOTE1")); 
              			JSch jsch=new JSch();
              			Session session=jsch.getSession(serverUserName, serverAddress, serverPort);
              			session.setPassword(masterPassword);
              			localUserInfo lui=new localUserInfo();
              			session.setUserInfo(lui);
              			session.connect();
              			session.setPortForwardingL(portforwardsLOCAL1,serverInternalAddress,portforwardsREMOTE1);
              			session.setPortForwardingL(portforwardsLOCAL2,serverInternalAddress,portforwardsREMOTE2);
              			System.out.println("Connected");
              			
          			}
          	}

      class localUserInfo implements UserInfo{
    	  String passwd;
    	  public String getPassword(){ return passwd; }
    	  public boolean promptYesNo(String str){return true;}
    	  public String getPassphrase(){ return null; }
    	  public boolean promptPassphrase(String message){return true; }
    	  public boolean promptPassword(String message){return true;}
    	  public void showMessage(String message){}
      }  
